﻿Imports DNP.GestionComercial.FormBase
Imports DNP.GestionComercial.Util
Imports DNP.GestionComercial.BE
Imports DNP.GestionComercial.BL
Imports Scripting

Public Class frmVentas
    Inherits frmFormBasePrincipal
#Region "variables"
    Dim oMsg As New sisMensaje
    Private obeVenta As New VentaBE
    Dim vProducto As ProductoBE
    Dim oVenta As New VentaBE
    Public vFormBase As frmFormBase = New frmFormBase
#End Region
#Region "Operaciones"
    Private Sub recuperarDatos()
        oVenta.NVEN_CODIGO = IIf(txtCodVenta.Text = "", 0, Val(txtCodVenta.Text))
        oVenta.NVEN_TOTAL = lblRptaValorVenta.Text
    End Sub
#End Region

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        If Not validarDatos() Then
            vFormBase.showError()
            vFormBase.objListaError = New TError
            Return
        End If
        Dim vVentaDetalle As New VentaDetalleBE
        vVentaDetalle.NPRD_CODIGO = txtCodProducto.Text
        vVentaDetalle.DESCRIPCION_PRODUCTO = txtDescripProducto.Text
        vVentaDetalle.NVEN_CANTIDAD = Val(txtDec_Cantidad.Text)
        Dim pParametro As New PARAMETROBE
        pParametro.PARAMETRO01 = vVentaDetalle.NPRD_CODIGO
        pParametro.PARAMETRO02 = vVentaDetalle.NVEN_CANTIDAD
        vVentaDetalle.NVEN_PRECIO_UNI = New ProductoBL().ObtenerPrecio(pParametro).ValorRertono
        vVentaDetalle.NVEN_PRECIO = vVentaDetalle.NVEN_PRECIO_UNI * vVentaDetalle.NVEN_CANTIDAD
        vVentaDetalle.CODIGO_BARRA = txtCodProducto.Text
        oVenta.DETALLE.Add(vVentaDetalle)
        With vVentaDetalle
            Dim item As ListViewItem
            item = New ListViewItem(vVentaDetalle.NPRD_CODIGO.ToString)
            item.SubItems.Add(vVentaDetalle.DESCRIPCION_PRODUCTO.ToString)
            item.SubItems.Add(vVentaDetalle.NVEN_PRECIO_UNI.ToString)
            item.SubItems.Add(vVentaDetalle.NVEN_CANTIDAD.ToString)
            item.SubItems.Add(vVentaDetalle.NVEN_PRECIO.ToString)
            lvDetalle.Items.Add(item)
        End With
        calculartotal()

    End Sub
    Private Sub calculartotal()
        Dim vVentaTotal As Double = 0
        Dim vIGV As Double = 0
        Dim vTotal As Double = 0
        For Each vDetalleVenta As VentaDetalleBE In oVenta.DETALLE
            vVentaTotal = vVentaTotal + vDetalleVenta.NVEN_PRECIO
        Next
        lblRptaValorVenta.Text = vVentaTotal.ToString
        colorearListView(lvDetalle)
    End Sub
    Private Function validarDatosVenta() As Boolean

        If oVenta.DETALLE.Count <= 0 Then
            vFormBase.objListaError.intNumError += 1
            vFormBase.objListaError.strCampoError = "Lista de Productos"
            Return False
        End If
        Return True
    End Function
    Private Function validarDatos() As Boolean
        If vFormBase.ValidarStringVacio(txtCodigoBarra.Text, lblProducto.Text, True) = "" Then
            txtCodigoBarra.Focus()
            Return False
        End If
        If vFormBase.ValidarDecimalVacio(txtDec_Cantidad.Text, lblCantidad.Text, True) = 0 Then
            txtDec_Cantidad.Focus()
            Return False
        End If
        If vFormBase.ValidarDecimalVacio(txtPrecio.Text, "Precio", True) = 0 Then
            txtPrecio.Text = 0

        End If
        Return True
    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If MsgBox("Esta seguro de eliminar el registro seleccionado", MsgBoxStyle.YesNo) = vbYes Then
            For Each i As ListViewItem In lvDetalle.SelectedItems
                oVenta.DETALLE.RemoveAt(i.Index)
                lvDetalle.Items.Remove(i)

            Next
            calculartotal()
        End If

    End Sub



    Private Sub frmVentas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        inicializarControles()

    End Sub
    Private Sub inicializarControles()
        lblFecha.Text = FormatDateTime(Date.Now, vbLongDate).ToUpper
        lblTitulo.Text = "Ventas"
        '  SplitContainer1.SplitterDistance = 300
        lblRptaValorVenta.Text = "0.00"
        txtPrecio.Hide()
        txtCodUnidadVenta.Hide()
        txtCodProducto.Hide()
        txtCodigoBarra.Focus()
        lblNombreUnidadVta.Text = ""

    End Sub

    Private Sub lvDetalle_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvDetalle.KeyDown
        If e.KeyValue = sisEnum.TeclasComunes.EliminarRegistro Then
            btnEliminar_Click(Nothing, Nothing)
        End If
    End Sub


    Private Sub txtCodigoBarra_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigoBarra.KeyPress
        If e.KeyChar = Chr(13) Then
            vProducto = New ProductoBL().listarProductoxCodigoBarra(txtCodigoBarra.Text).ValorRertono
            txtCodigoBarra.Text = Convert.ToString(vProducto.CPRD_CODIGOBARRA) ' codigo de barra
            txtDescripProducto.Text = Convert.ToString(vProducto.CPRD_DESCRIPCION)
            txtCodProducto.Text = Convert.ToString(vProducto.NPRD_CODIGO)
            lblNombreUnidadVta.Text = Convert.ToString(vProducto.DES_UNIDAD_VENTA)
            txtCodUnidadVenta.Text = CStr(vProducto.COM_UNIDAD_VENTA)
            txtDec_Cantidad.Focus()
        End If
    End Sub

    Private Sub txtCodigoBarra_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCodigoBarra.KeyDown
        If e.KeyValue = sisEnum.TeclasComunes.AbrirFormBusqueda Then
            Dim buscarProducto As New frmBusProducto()
            buscarProducto.ShowDialog()
            If (buscarProducto.DialogResult = Windows.Forms.DialogResult.OK) Then
                txtCodigoBarra.Text = Convert.ToString(buscarProducto.vRespuesta.PARAMETRO06) ' codigo de barra
                txtDescripProducto.Text = Convert.ToString(buscarProducto.vRespuesta.PARAMETRO02)
                txtCodProducto.Text = Convert.ToString(buscarProducto.vRespuesta.PARAMETRO01)
                lblNombreUnidadVta.Text = Convert.ToString(buscarProducto.vRespuesta.PARAMETRO04)
                txtDec_Cantidad.Focus()
            End If
        End If
    End Sub

  


    Public Sub colorearListView(ByRef list As ListView)

        Dim i As Integer
        For i = 0 To list.Items.Count - 1
            If i = Int(i / 2) * 2 Then
                list.Items.Item(i).BackColor = Color.White
            Else
                list.Items.Item(i).BackColor = Color.LightGreen
            End If
        Next
        list.FullRowSelect = True
    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        vFormBase.limpiarControlesForm(Me)
        lblNombreUnidadVta.Text = ""
    End Sub

    Private Sub txtDec_Cantidad_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDec_Cantidad.KeyPress
        If e.KeyChar = Chr(13) Then
            btnAgregar_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
        Imprimir()
    End Sub
    Private Sub Imprimir()
        Try
            Dim objFSO As FileSystemObject
            Dim objStream As Object
            objFSO = CreateObject("Scripting.FileSystemObject")
            ' objStream = objFSO.CreateTextFile("Canon MP280 series Printer")
            objStream = objFSO.CreateTextFile("C:\Aplicaciones\MiTicket.txt")

            'TIPO DE LEGRA DRAFT
            objStream.Writeline(Chr(27) & Chr(120) & Chr(48) & Chr(7) & Chr(15) & Chr(7))
            For i As Integer = 1 To 40
                Dim Texto As String = "ITEM NRO: " + i.ToString()
                Dim Strim = (Texto)
                objStream.WriteLine(Strim)
            Next

            For i As Integer = 1 To 40
                Dim Texto As String = "ITEM NRO: " + i.ToString()
                Dim Strim = (Texto)
                objStream.WriteLine(Strim)
            Next

            'LIMPIAR BUFFER
            objStream.Writeline(Chr(18) & Chr(7))
            objStream.WriteLine("")
            objStream.WriteLine("")
            objStream.WriteLine(Space(3) & "  ***** GRACIAS POR SU COMPRA *****")
            'FOR PARA SALIR PAPEL Y PODER CORTAR
            For XX = 0 To 10
                objStream.WriteLine("")
            Next
            'CORTE DE PAGINA
            objStream.writeline(Chr(27) & Chr(100) & Chr(0))
            'CIERRE
            objStream.Close()
            objFSO = Nothing
            objStream = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Aviso Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub btnPagar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPagar.Click
        If Not validarDatosVenta() Then
            vFormBase.showError()
            vFormBase.objListaError = New TError
            Return
        End If
        recuperarDatos()
        Dim frm As New frmPagarVenta(oVenta, Me)
        frm.Owner = Me
        frm.ShowDialog()

        If (frm.DialogResult = Windows.Forms.DialogResult.OK) Then
            btnAgregar.Visible = False
        End If

    End Sub
  
End Class